﻿/*
Description                     AD LdapSyntax               RFC 2252 Syntax

Boolean                         2.5.5.8                     1.3.6.1.4.1.1466.115.121.1.7
Enumeration                     2.5.5.9                     1.3.6.1.4.1.1466.115.121.1.27
Integer                         2.5.5.9                     1.3.6.1.4.1.1466.115.121.1.27
LargeInteger                    2.5.5.16                    1.2.840.113556.1.4.906

Object(Access-Point)            2.5.5.14                    1.3.6.1.4.1.1466.115.121.1.2
Object(DN-Binary)               2.5.5.7                     1.2.840.113556.1.4.903
Object(DN-String)               2.5.5.14                    1.2.840.113556.1.4.904
Object(DS-DN)                   2.5.5.1                     1.3.6.1.4.1.1466.115.121.1.12
Object(OR-Name)                 2.5.5.7                     1.2.840.113556.1.4.1221
Object(Presentation-Address)    2.5.5.13                    1.3.6.1.4.1.1466.115.121.1.43

String(Case)                    2.5.5.3                     1.2.840.113556.1.4.1362
String(Generalized-Time)        2.5.5.11                    1.3.6.1.4.1.1466.115.121.1.24
String(IA5)                     2.5.5.5                     1.3.6.1.4.1.1466.115.121.1.26
String(NT-Sec-Desc)             2.5.5.15                    1.2.840.113556.1.4.907
String(Numeric)                 2.5.5.6                     1.3.6.1.4.1.1466.115.121.1.36
String(Object-Identifier)       2.5.5.2                     1.3.6.1.4.1.1466.115.121.1.38
String(Octet)                   2.5.5.10                    1.3.6.1.4.1.1466.115.121.1.40
String(Printable)               2.5.5.5                     1.3.6.1.4.1.1466.115.121.1.44
String(Sid)                     2.5.5.17                    1.3.6.1.4.1.1466.115.121.1.40
String(Teletex)                 2.5.5.4                     1.2.840.113556.1.4.905
String(Unicode)                 2.5.5.12                    1.3.6.1.4.1.1466.115.121.1.15
String(UTC-Time)                2.5.5.11                    1.3.6.1.4.1.1466.115.121.1.53

*/

namespace DirectoryServicesProtocolsExtensions.LdapConnection.MetaData
{
    using System;

    public static class LdapTypeToRowDotNetTypeMapper
    {
        // System.DirectoryServices.Protocols packages LDAP values as either string or byte[]
        // no int, bool, etc
        public static Type GetRawDotNetType(string attributeSyntax)
        {
            switch (attributeSyntax)
            {
                // String based attributes
                case "1.3.6.1.4.1.1466.115.121.1.12":
                case "1.3.6.1.4.1.1466.115.121.1.15":
                case "1.3.6.1.4.1.1466.115.121.1.38":     
                case "1.2.840.113556.1.4.1362":           
                case "1.2.840.113556.1.4.905":            
                case "1.3.6.1.4.1.1466.115.121.1.26":                             
                case "1.3.6.1.4.1.1466.115.121.1.36":
                // String(Generalized-Time)
                case "1.3.6.1.4.1.1466.115.121.1.24":
                // String(UTC Generalized-Time)
                case "1.3.6.1.4.1.1466.115.121.1.53":
                // LargeInteger
                // It appears that DirectoryServicesProtocols packages this type in the result as string and not
                // as one would expect int64, they way it is stored in AD
                // Integer and Enum (ex. userAccountControl
                case "1.3.6.1.4.1.1466.115.121.1.27": 
                case "1.2.840.113556.1.4.906":
                    return Type.GetType("System.String");
                        
                // Boolean 
                // todo: this needs to be converted to string
                // todo: add test 
                case "1.3.6.1.4.1.1466.115.121.1.7":     
                    return Type.GetType("System.Boolean");

                    
                // String(Octed) and String(Sid)
                case "1.3.6.1.4.1.1466.115.121.1.40":    
                // NT Security Descriptor
                case "1.2.840.113556.1.4.907":
                    return Type.GetType("System.Byte[]");
                          
                default:
                    throw new Exception("Unexpcted Active Directory Schema Syntax detected:" + attributeSyntax);
            }
        }
    }
}
